package service;

import dao.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Admin;
import pojo.Bed;
import pojo.ChangeRoom;
import pojo.CheckOutRecord;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.jar.JarEntry;

public class ChangeRoomServiceImpl implements ChangeRoomService{
    private ChangeRoomDao dao = new ChangeRoomDaoImpl();
    private BedDao bedDao = new BedDaoImpl();
    private Log log = LogFactory.getLog(getClass());

    @Override
    public ArrayList<ChangeRoom> findAll() {
        return dao.findAll();
    }

    @Override
    public ArrayList<ChangeRoom> findDate(Date begin, Date end) {
        ArrayList<ChangeRoom> list = dao.findAll();
        ArrayList<ChangeRoom> changeRooms = new ArrayList<ChangeRoom>();
        for (ChangeRoom changeRoom : list) {
            if((changeRoom.getChange_time().after(begin)&&changeRoom.getChange_time().before(end))||changeRoom.getChange_time().equals(begin)||changeRoom.getChange_time().equals(end)){
                changeRooms.add(changeRoom);
            }
        }
        return changeRooms;
    }

    @Override
    public ChangeRoom findById(Integer changeId) {
        return dao.findById(changeId);
    }


    @Override
    public int insert(ChangeRoom changeRoom) {
        /**
         * 插入一条换房信息，则需要
         *      1. 将原来床铺状态置为false
         *      2. 将现在的床铺状态值为true
         *      3. 更新入住信息表
         */
        log.info(SystemEnum.BEGIN);
        PreparedStatement ps = null;
        PreparedStatement ps2 = null;
        Connection con = null;
        try {

            con = JDBCUtils.getConnection();
            con.setAutoCommit(false);

            //获取两张床
            Bed oldBed = bedDao.findById(changeRoom.getOld_dormId(),changeRoom.getOld_bedId());
            Bed newBed = bedDao.findById(changeRoom.getNew_dormId(),changeRoom.getNew_bedId());
            System.out.println(oldBed+"\n"+newBed);
            //转换两张床的状态
            int row1 = bedDao.update(con,oldBed);
            int row2 = bedDao.update(con,newBed);

            //更新入住信息
            int row3 = 0;
            String sql2 = "UPDATE check_in_record SET dormId = ?,bedId = ? WHERE check_in_id = ?";
            ps = con.prepareStatement(sql2);
            ps.setInt(1,changeRoom.getNew_dormId());
            ps.setInt(2,changeRoom.getNew_bedId());
            ps.setInt(3,changeRoom.getCheck_in_id());
            row3 = ps.executeUpdate();

            //添加换房信息
            int row4 = 0;
            String sql3 = "INSERT INTO change_record (stuId,old_dormId,old_bedId,new_dormId,new_bedId,change_time,check_in_id) VALUES (?,?,?,?,?,?,?)";
            ps2 = con.prepareStatement(sql3);
            ps2.setInt(1,changeRoom.getStuId());
            ps2.setInt(2,changeRoom.getOld_dormId());
            ps2.setInt(3,changeRoom.getOld_bedId());
            ps2.setInt(4,changeRoom.getNew_dormId());
            ps2.setInt(5,changeRoom.getNew_bedId());
            ps2.setDate(6,new java.sql.Date(changeRoom.getChange_time().getTime()));
            ps2.setInt(7,changeRoom.getCheck_in_id());
            row4 = ps2.executeUpdate();

            con.commit();
            log.info(SystemEnum.SUCCESS);
            return row1+row2+row3+row4;
        } catch (SQLException e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE+"\t正在回滚");
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                con.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            log.info(SystemEnum.END);
        }
        return 0;
    }

    @Override
    public int update(ChangeRoom changeRoom) {
        return dao.update(changeRoom);
    }

    @Override
    public int delete(ChangeRoom changeRoom) {
        return dao.delete(changeRoom);
    }
}
